package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.List;

/**
 * @author ：Shijiu
 * @create  2019/09/08 20:18
 * @description ：权限的controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        return permissionService.findByPage(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true, "新增权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增权限失败");
        }
    }

    @RequestMapping("/findPermissionById")
    public Result findPermissionById(Integer id){
        try {
            Permission permission=permissionService.findPermissionById(id);
            return new Result(true,"查询成功",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Permission permission){
        try {
            permissionService.update(permission);
            return new Result(true,"编辑权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑权限失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            permissionService.delete(id);
            return new Result(true, "删除成功");
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    @RequestMapping("/findAllPermission")
    public Result findAllPermission(){
        try {
            //查询所有的权限
            List<Permission> permissionList=permissionService.findAllPermission();
            return new Result(true, "权限列表查询成功",permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "权限列表查询失败");
        }

    }
}
